package com.crm.controller.reportsController;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.OpportunityDAO;
import com.crm.entity.Business;
import com.crm.entity.Opportunity;
import com.crm.util.Constants;

@Controller
@RequestMapping("/pendingEnquiry")
public class PendingEnqController {
	protected final Log logger = LogFactory.getLog(getClass());
	@RequestMapping( "/show.do") 
	public ModelAndView show(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		//OpportunityDAO odao=new OpportunityDAO();
		String status=Constants.ST_PENDING;
		List<Opportunity>  lop=null;
		try {
			lop=OpportunityDAO.getSingleInstance().getByStatus(status, busid);
		} catch(NullPointerException npe){
			logger.info("Null Pointer Exception Occured");
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return model=new ModelAndView("/jsp/pending-enquiries.jsp")
		.addObject("submenu", "reports")
		.addObject("menuName","reports")
		.addObject("opplist",lop);
	}
	@RequestMapping( "/update.do") 
	public ModelAndView update(HttpServletRequest request,HttpSession session, ModelAndView model) throws Exception{
		Business bus=(Business)session.getAttribute("business");
		if(bus==null)return new ModelAndView("/jsp/index.jsp");
		int busid=bus.getBusinessId();
		//OpportunityDAO oppdao=new OpportunityDAO();
		String srowCount=request.getParameter("rowCount");
		if (srowCount!=null) {
			int rowCount = Integer.parseInt(srowCount);
			for (int i = 0; i < rowCount; i++) {
				String remarks = request.getParameter("remarks" + (i + 1));
				String refno = request.getParameter("refNo" + (i + 1));
				OpportunityDAO.getSingleInstance().updateRemarks(refno, remarks, busid);
			}
		}
		return model=new ModelAndView("redirect:show.do")
		.addObject("msg","Successfully Updated Remarks");
	}

}
